/*
 * 文件名：IncaeSureController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月21日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.mgr.bpa.domain.IncaeSureAssist;
import top.infopub.mgr.bpa.domain.IncaeSureDomain;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.bpa.vo.IncaeSureVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.mgr.util.FileHelper;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.bpa.IncaeSureAtt;
import top.infopub.model.bpa.IncaeSureDetail;
import top.infopub.model.bpa.IncaeSureList;
import top.infopub.model.bpa.IncaeSureSub;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;


/**确权支付
 * @author wuzhiyang
 * @version 2020年2月21日
 * @see IncaeSureController
 * @since商务管理/确权支付/对内建安产值确权
 */
@Controller
@RequestMapping("/incaeSure")
public class IncaeSureController extends GenericFlowController {

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private InnerAgrtService innerAgrtService;

    @Autowired
    private SubpackCotService subpackCotService;

    @Autowired
    private SubpackCotListService subpackCotListService;

    @Autowired
    private SubpackCotDetailService subpackCotDetailService;

    @Autowired
    private InnerAgrtDetailService innerAgrtDetailService;

    @Autowired
    private IncaeSureDetailService incaeSureDetailService;

    @Autowired
    private IncaeSureListService incaeSureListService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * 对内建安产值确权列表
     * @return 
     * @see
     */
    @RequestMapping("/list")
    public String list() {
        return "bpa/lay-incaeSure-list";
    }

    /**
     * 对内建安产值确权列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<IncaeSureDomain> queryDataByCondition(IncaeSureVo incaeSureVo,
                                                       ParamPage paramPage,
                                                       String monthStartDateStr,
                                                       String monthEndDateStr) {
        Page<IncaeSureDomain> page = new Page<IncaeSureDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (incaeSureVo.getEndDate() != null) {
            incaeSureVo.setEndDate(DateUtils.addDays(incaeSureVo.getEndDate(), 1));
        }
        //查询时，确权开始日期增加一天
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            incaeSureVo.setStartSureDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，确权结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            incaeSureVo.setEndSureDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        page = incaeSureService.selectByDomainWhere(page, incaeSureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对外建安产值确权-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<IncaeSureDomain> queryMyCheckDataByCondition(IncaeSureVo incaeSureVo,
                                                              ParamPage paramPage,
                                                              String monthStartDateStr,
                                                              String monthEndDateStr) {
        Page<IncaeSureDomain> page = new Page<IncaeSureDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (incaeSureVo.getEndDate() != null) {
            incaeSureVo.setEndDate(DateUtils.addDays(incaeSureVo.getEndDate(), 1));
        }
        //查询时，确权开始日期
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            incaeSureVo.setStartSureDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，确权结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            incaeSureVo.setEndSureDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        incaeSureVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = incaeSureService.selectMyCheckByDomainWhere(page, incaeSureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对内建安产值确权-新增
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "bpa/lay-incaeSure-add";
    }

    /**
     * 根据合同查询相关带入信息(项目名称_合同名称)
     * @param request
     * @return
     * @throws Exception
     */
//    @RequestMapping("/getIncaeSureByTypes")
//    @ResponseBody
//    public Map<String, Object> getIncaeSureByTypes(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            //接收前端传过来的合同类型
//            String types = request.getParameter("types");
//            if ("1".equals(types)) {
//                //返回所有内部协议中的项目名称_合同名称
//                List<InnerAgrt> InnerAgrtList = innerAgrtService.selectInnerAgrt();
//                map.put("InnerAgrtList", InnerAgrtList);
//            }
//            else if ("2".equals(types)) {
//                //返回所有分包合同中的项目名称_合同名称
//                List<SubpackCot> selectSubpackCotList = subpackCotService.selectSubpackCot();
//                map.put("selectSubpackCotList", selectSubpackCotList);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询项目名称记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据分部分项名称和refId查询月累金额
     * @param id
     * @return
     */
    @RequestMapping("/getMonthAfterAmount")
    @ResponseBody
    public Map<String, Object> getMonthAfterAmount(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String proCode = request.getParameter("proCodeAndCodes");
            String sureMonth = request.getParameter("sureMonth");
            Date month = DateUtils.parseDate(sureMonth, "yyyy-MM");
            String[] split = proCode.split("\\_");
            Map<String, Object> whereMap = new HashMap<String, Object>();
            whereMap.put("proCode", split[0]);
            whereMap.put("sureMonth", month);
            //当月对外确权金额(下浮后)元(对外建安)
            BigDecimal outcaeSure = outcaeSureService.selectBySure(whereMap);
            if (outcaeSure == null) {
                outcaeSure = BigDecimal.ZERO;
            }
            map.put("outcaeSure", outcaeSure);
            //所有月份合计(对外确权金额(下浮后)元)(对外建安)
            BigDecimal bigDecimal = outcaeSureService.selectDecimalByProCode(whereMap);
            if (bigDecimal == null) {
                bigDecimal = BigDecimal.ZERO;
            }
            map.put("monthAfterAmount", bigDecimal);
        }
        catch (Exception e) {
            log.error("查询对内建安产值确权记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }

    /**
     * 根据内部协议和分包合同中项目编号查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
//    @RequestMapping("/getIncaeSureByProCode")
//    @ResponseBody
//    public Map<String, Object> getOutcaeSureByProCode(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCode = request.getParameter("proCodeAndCodes");
//        String types = request.getParameter("types");
//        String[] split = proCode.split("\\_");
//        try {
//            Map<String, Object> mapWhere = new HashMap<String, Object>();
//            mapWhere.put("refId", split[1]);
//            mapWhere.put("processStatus", "9");
//            mapWhere.put("orderCondition", "ADD_TIME desc");
//            if ("1".equals(types)) {
//                //带出内部协议的信息
//                InnerAgrtDomain innerAgrtDomain = new InnerAgrtDomain();
//                List<InnerAgrt> innerAgrts = innerAgrtService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(innerAgrts)) {
//                    innerAgrtDomain = innerAgrtService.selectByIds(innerAgrts.get(0).getId());
//                }
//                else {
//                    innerAgrtDomain = innerAgrtService.selectByIds(split[1]);
//                }
//                if (innerAgrtDomain != null) {
//                    map.put("InnerAgrt", innerAgrtDomain);
//                    //带出 合同清单子项  和  合同清单子项费用明细  信息
//                    List<InnerAgrtDetailAndSubDomain> byInnerAgrtIdList = innerAgrtDetailService.selectByInnerAgrtId(innerAgrtDomain.getId());
//                    map.put("innerAgrtDetailAndSubList", byInnerAgrtIdList);
//                    //带出 合同清单子项  和  合同清单子项费用明细  信息
//                    List<InnerAgrtDetailAndSubDomain> innerAgrtIdList = innerAgrtDetailService.selectByInnerAgrtId(innerAgrtDomain.getId());
//                    List<InnerAgrtDetailAndSubDomain> list = new ArrayList<>();
//                    for (InnerAgrtDetailAndSubDomain iSubDomain : innerAgrtIdList) {
//                        BigDecimal addupAmountTotal = incaeSureDetailService.selectAddupAmountBySubNameAndRefId(
//                            iSubDomain.getSubName(), split[1], iSubDomain.getTypeName());
//                        if (addupAmountTotal == null) {
//                            iSubDomain.setAddupAmountTotal(BigDecimal.ZERO);
//                        }
//                        iSubDomain.setAddupAmountTotal(addupAmountTotal);
//                        list.add(iSubDomain);
//                    }
//                    map.put("innerAgrtDetailAndSubList", list);
//                }
//                else {
//                    flag = false;
//                }
//            }
//            else if ("2".equals(types)) {
//                SubpackCotDomain subpackCotDomain = new SubpackCotDomain();
//                List<SubpackCot> subpackCots = subpackCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(subpackCots)) {
//                    subpackCotDomain = subpackCotService.selectById(subpackCots.get(0).getId());
//                }
//                else {
//                    subpackCotDomain = subpackCotService.selectById(split[1]);
//                }
//                if (subpackCotDomain != null) {
//                    map.put("subpackCotDomain", subpackCotDomain);
//                    //带出 合同清单子项  和  合同清单子项费用明细  信息
//                    List<SubpackCotDetailAndSubDomain> subpackCotIdList = subpackCotDetailService.selectBySubpackCotId(subpackCotDomain.getId());
//                    List<SubpackCotDetailAndSubDomain> list = new ArrayList<>();
//                    for (SubpackCotDetailAndSubDomain iSubDomain : subpackCotIdList) {
//                        BigDecimal addupAmountTotal = incaeSureDetailService.selectAddupAmountBySubNameAndRefId(
//                            iSubDomain.getSubName(), split[1], iSubDomain.getTypeName());
//                        if (addupAmountTotal == null) {
//                            iSubDomain.setAddupAmountTotal(BigDecimal.ZERO);
//                        }
//                        iSubDomain.setAddupAmountTotal(addupAmountTotal);
//                        list.add(iSubDomain);
//                    }
//                    map.put("subpackCotDetailAndSubList", list);
//                    //带出清单模式
//                    List<SubpackCotListDomain> subpackCotLists = subpackCotListService.selectBySubpackCotId(subpackCotDomain.getId());
//                    List<SubpackCotListDomain> list2 = new ArrayList<>();
//                    for (SubpackCotListDomain sDomain : subpackCotLists) {
//                        BigDecimal nameAndRefId = incaeSureListService.selectAddupAmountBySubNameAndRefId(
//                            sDomain.getSubName(), split[1]);
//                        if (nameAndRefId == null) {
//                            sDomain.setAddupAmountTotal(BigDecimal.ZERO);
//                        }
//                        sDomain.setAddupAmountTotal(nameAndRefId);
//                        list2.add(sDomain);
//                    }
//                    map.put("subpackCotLists", list2);
//                }
//                else {
//                    flag = false;
//                }
//            }
//        }
//        catch (Exception e) {
//            log.error("查询分包合同或内部协议记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据分部分项名称和refId查询月累金额
     * @param id
     * @return
     */
    @RequestMapping("/getAddupAmountBySubNameAndRefId")
    @ResponseBody
    public Map<String, Object> getAddupAmountBySubNameAndRefId(String subName,
                                                               String proCodeAndCodes,
                                                               String typeName) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(subName) || StringUtils.isBlank(proCodeAndCodes)
            || StringUtils.isBlank(typeName)) {
            flag = false;
        }
        else {
            try {
                String[] split = proCodeAndCodes.split("\\_");
                String refId = split[1];
                BigDecimal addupAmountTotal = incaeSureDetailService.selectAddupAmountBySubNameAndRefId(
                    subName, refId, typeName);
                map.put("addupAmountTotal", addupAmountTotal);
            }
            catch (Exception e) {
                log.error("查询对内建安产值确权记录异常", e);
                flag = false;
            }
        }
        map.put("flag", flag);
        return map;
    }

    /**
     * 根据分部分项名称和refId查询月累金额
     * @param id
     * @return
     */
    @RequestMapping("/getAddupAmount")
    @ResponseBody
    public Map<String, Object> getAddupAmount(String subName, String proCodeAndCodes) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(subName) || StringUtils.isBlank(proCodeAndCodes)) {
            flag = false;
        }
        else {
            try {
                String[] split = proCodeAndCodes.split("\\_");
                String refId = split[1];
                BigDecimal nameAndRefId = incaeSureListService.selectAddupAmountBySubNameAndRefId(
                    subName, refId);
                map.put("addupAmountTotal", nameAndRefId);
            }
            catch (Exception e) {
                log.error("查询清单模式记录异常", e);
                flag = false;
            }
        }
        map.put("flag", flag);
        return map;
    }

    /**
     * 对外建安产值确权-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(IncaeSureAssist incaeSureAssist)
        throws ParseException {

        //主表-对内建安产值确权
        incaeSureAssist.setHrDeptId(incaeSureAssist.getHrDeptid());
        incaeSureAssist.setHrOrgId(incaeSureAssist.getHrOrgid());
        incaeSureAssist.setStaffName(incaeSureAssist.getStaffname());
        incaeSureAssist.setHrStaffApplyId(incaeSureAssist.getHrStaffApplyid());
        String proCode = "";
        String code = "";
        String[] strCodes = incaeSureAssist.getProCodeAndCodes().split("\\_");
        proCode = strCodes[0];
        code = strCodes[1];
        incaeSureAssist.setRefId(code);
        incaeSureAssist.setProCode(proCode);
        Date month = DateUtils.parseDate(incaeSureAssist.getSureMonthStr(), "yyyy-MM");
        incaeSureAssist.setSureMonth(month);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        incaeSureAssist.setId(id);
        incaeSureAssist.setAddBy(vo.getId());
        incaeSureAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("bpa_incae_sure");
        incaeSureAssist.setCodes(codes);
        // 处理processId
        incaeSureAssist.setProcessId(IdFactory.getUUIDUpper());
        incaeSureAssist.setProcessStatus("1");//审批中
        incaeSureAssist.setTypes("1");//PPP项目
        incaeSureAssist.setUptBy(vo.getId());
        incaeSureAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //新增对内建安产值确权分解子项
        List<IncaeSureSub> incaeSureSubs = new ArrayList<IncaeSureSub>();
        //对外建安产值确权清单分解子项费用明细
        List<IncaeSureDetail> incaeSureDetails = new ArrayList<IncaeSureDetail>();
        //子项名称
        String[] subNames = incaeSureAssist.getSubNames();
        if (subNames != null && subNames.length > 0) {
            for (int i = 0; i < subNames.length; i++ ) {
                String subName = subNames[i];
                if (StringUtils.isEmpty(subName) && StringUtils.isEmpty(subName)) {
                    continue;
                }
                IncaeSureSub incaeSureSub = new IncaeSureSub();
                String incaeSureSubId = IdFactory.getUUIDUpper();//自动生成主键
                incaeSureSub.setId(incaeSureSubId);
                incaeSureSub.setIncaeSureId(id);
                incaeSureSub.setSubName(subName);
                incaeSureSub.setAddBy(vo.getId());
                Date addTime = top.infopub.mgr.util.DateUtils.addSeconds(
                    top.infopub.mgr.util.DateUtils.getCurrentDateTime(), i);
                incaeSureSub.setAddTime(addTime);
                incaeSureSubs.add(incaeSureSub);
            }

            //费用大类
            String[] feeSmallTypes = incaeSureAssist.getFeeSmallTypes();
            for (int j = 0; j < feeSmallTypes.length; j++ ) {
                String feeSmallType = feeSmallTypes[j];
                String itemNameSub = incaeSureAssist.getItemNameSub()[j];
                String typeName = incaeSureAssist.getTypeNames()[j];
                String orderIdx = incaeSureAssist.getOrderIdxs()[j];
                String inAfterAmount = incaeSureAssist.getInAfterAmounts()[j];
                String monthPreAmount = incaeSureAssist.getMonthPreAmounts()[j];
                String monthAfterAmount = incaeSureAssist.getMonthAfterAmounts()[j];
                String applyPreAmount = incaeSureAssist.getApplyPreAmounts()[j];
                String addupAfterAmount = incaeSureAssist.getAddupAfterAmounts()[j];
                String inDownRate = incaeSureAssist.getInDownRates()[j];
                String notDownAmount = incaeSureAssist.getNotDownAmounts()[j];
                if (StringUtils.isEmpty(feeSmallType)) {
                    continue;
                }
                IncaeSureDetail incaeSureDetail = new IncaeSureDetail();
                String incaeSureDetailId = IdFactory.getUUIDUpper();//自动生成主键
                incaeSureDetail.setId(incaeSureDetailId);

                incaeSureDetail.setSureSubId(itemNameSub);

                incaeSureDetail.setIncaeSureId(id);
                incaeSureDetail.setOrderIdx(orderIdx);
                incaeSureDetail.setTypeName(typeName);
                String[] split = orderIdx.split("\\.");
                if (split.length == 1) {
                    incaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType));
                }
                else if (split.length == 2) {
                    String feeSmallType2 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    incaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType2));
                }
                else if (split.length == 3) {
                    String feeSmallType3 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    incaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType3));
                }
                incaeSureDetail.setInAfterAmount(new BigDecimal(inAfterAmount));
                incaeSureDetail.setMonthPreAmount(new BigDecimal(monthPreAmount));
                incaeSureDetail.setMonthAfterAmount(new BigDecimal(monthAfterAmount));
                incaeSureDetail.setApplyPreAmount(new BigDecimal(applyPreAmount));
                incaeSureDetail.setAddupAfterAmount(new BigDecimal(addupAfterAmount));
                incaeSureDetail.setInDownRate(new BigDecimal(inDownRate));
                incaeSureDetail.setNotDownAmount(new BigDecimal(notDownAmount));
                incaeSureDetail.setAddBy(vo.getId());
                incaeSureDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                incaeSureDetails.add(incaeSureDetail);
            }

            for (IncaeSureDetail incaeSureDetail : incaeSureDetails) {
                for (IncaeSureSub incaeSureSub : incaeSureSubs) {
                    if (incaeSureDetail.getSureSubId().equals(incaeSureSub.getSubName())) {
                        incaeSureDetail.setSureSubId(incaeSureSub.getId());
                        break;
                    }
                }
            }
        }

        //清单模式
        List<IncaeSureList> incaeSureLists = new ArrayList<IncaeSureList>();
        String[] showIdxLists = incaeSureAssist.getShowIdxLists();
        if ("2".equals(incaeSureAssist.getSureType()) && showIdxLists != null
            && showIdxLists.length > 0) {
            for (int i = 0; i < showIdxLists.length; i++ ) {
                String showIdxList = showIdxLists[i];
                String subNameList = incaeSureAssist.getSubNameLists()[i];
                String subUnitList = incaeSureAssist.getSubUnitLists()[i];
                String cotNumList = incaeSureAssist.getCotNumLists()[i];
                String cotUnitPriceList = incaeSureAssist.getCotUnitPriceLists()[i];
                String cotSumPriceList = incaeSureAssist.getCotSumPriceLists()[i];
                String applyNumList = incaeSureAssist.getApplyNumLists()[i];
                String applyAmountList = incaeSureAssist.getApplyAmountLists()[i];
                String sureNumList = incaeSureAssist.getSureNumLists()[i];
                String monthSureAmountList = incaeSureAssist.getMonthSureAmountLists()[i];
                String addupSureAmount = incaeSureAssist.getAddupSureAmounts()[i];

                IncaeSureList incaeSureList = new IncaeSureList();
                String incaeSureListId = IdFactory.getUUIDUpper();//自动生成主键
                incaeSureList.setId(incaeSureListId);
                incaeSureList.setIncaeSureId(id);
                incaeSureList.setShowIdx(Integer.parseInt(showIdxList));
                incaeSureList.setSubName(subNameList);
                incaeSureList.setSubUnit(subUnitList);
                incaeSureList.setCotNums(new BigDecimal(cotNumList));
                incaeSureList.setCotSumPrice(new BigDecimal(cotSumPriceList));
                incaeSureList.setCotUnitPrice(new BigDecimal(cotUnitPriceList));
                incaeSureList.setApplyNums(new BigDecimal(applyNumList));
                incaeSureList.setApplyAmount(new BigDecimal(applyAmountList));
                incaeSureList.setSureNums(new BigDecimal(sureNumList));
                incaeSureList.setMonthSureAmount(new BigDecimal(monthSureAmountList));
                incaeSureList.setAddupSureAmount(new BigDecimal(addupSureAmount));
                incaeSureList.setAddBy(vo.getId());
                incaeSureList.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                incaeSureLists.add(incaeSureList);
            }
        }

        //上传附件
        //1=计算稿
        List<IncaeSureAtt> incaeSureAtts = new ArrayList<IncaeSureAtt>();
        String[] fileAtt1 = incaeSureAssist.getFileAtts1();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                IncaeSureAtt incaeSureAtt = createFileAtt(vo, id, file);
                incaeSureAtt.setTypes("11");
                incaeSureAtts.add(incaeSureAtt);
            }
        }
        //2=签字扫描件
        String[] fileAtt2 = incaeSureAssist.getFileAtts2();
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                IncaeSureAtt incaeSureAtt = createFileAtt(vo, id, file);
                incaeSureAtt.setTypes("12");
                incaeSureAtts.add(incaeSureAtt);
            }
        }
        //3=其他附件
        String[] fileAtt3 = incaeSureAssist.getFileAtts3();
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                IncaeSureAtt incaeSureAtt = createFileAtt(vo, id, file);
                incaeSureAtt.setTypes("4");
                incaeSureAtts.add(incaeSureAtt);
            }
        }

        incaeSureService.saveIncaeSure(incaeSureAssist, incaeSureAtts, incaeSureLists,
            incaeSureSubs, incaeSureDetails);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "商务管理 -确权支付-对内建安产值确权：项目名="
                                                              + incaeSureAssist.getProName(), null);
        return RspVo.getSuccessResponse();
    }

    private IncaeSureAtt createFileAtt(AuthorizationVO vo, String incaeSureId, String file) {
        //资料信息数组
        String[] fileAttStr = file.split("\\|");
        IncaeSureAtt fileAtt = new IncaeSureAtt();
        if(StringUtils.isNotEmpty(fileAttStr[2])){
            fileAtt.setId(fileAttStr[2]);
        }else{
            fileAtt.setId(IdFactory.getUUIDSerialNumber());
        }
        fileAtt.setIncaeSureId(incaeSureId);//项目id
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
            fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 上传附件
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/uploadAtt")
    @ResponseBody
    public RspVo updateAtt(HttpServletRequest request) {
        List<String> returnFileStrList = new ArrayList<>();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
        if (fileMap != null) {
            for (String name : fileMap.keySet()) {
                MultipartFile file = fileMap.get(name);
                //资料类型
                String types = request.getParameter("types");
                if (StringUtils.isEmpty(types)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
                }
                //上传文件
                String fileId = IdFactory.getUUIDSerialNumber();
                //服务器资源路径
                String serverPath = "incaeSure";
                String filePath = FileHelper.upload(file, serverPath);
                returnFileStrList.add("|" + file.getOriginalFilename().trim() + "|" + fileId + "|"
                                      + filePath);
                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传确权支付-对内建安产值确权附件-文件名:"
                                                                      + filePath + "，文件路径:"
                                                                      + filePath, null);
            }
        }
        return RspVo.getSuccessResponse(returnFileStrList);
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 对内建安产值确权id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        IncaeSureVo incaeSureVo = incaeSureService.queryById(id);
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setProjectCode(incaeSureVo.getProCode());
        orgInfo.setState(1);
        List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
        if (orgInfos.size() > 0) {
            incaeSureVo.setOrgProName(orgInfos.get(0).getCompName());
            incaeSureVo.setHrOrgProId(orgInfos.get(0).getId());
            OrgInfo parentOrgInfo = new OrgInfo();
            parentOrgInfo.setId(orgInfos.get(0).getParentId());
            parentOrgInfo.setState(1);
            List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
            if (parentOInfos.size() > 0) {
                incaeSureVo.setOrgAreaName(parentOInfos.get(0).getCompName());
                incaeSureVo.setHrOrgAreaId(parentOInfos.get(0).getId());
            }
        }
        request.setAttribute("modelDetail", incaeSureVo);
        map.put("incaeSure", incaeSureVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, incaeSureVo.getProcessId());
        return new ModelAndView("bpa/lay-incaeSure-detail", map);
    }
    /**
     * 对内建安产值确权-复制单据
     * @return
     */
    @RequestMapping("/toCopy")
    public String toCopy(HttpServletRequest request) {
        String id = request.getParameter("id");
        IncaeSureVo incaeSureVo = incaeSureService.queryById(id);
        request.setAttribute("incaeSureVo", incaeSureVo);
        return "bpa/lay-incaeSure-copy";
    }
    /**
     * 根据对内建安确权id查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getIncaeSureById")
    @ResponseBody
    public Map<String, Object> getIncaeSureById(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String sguid = request.getParameter("sguid");
        try {
            IncaeSureVo incaeSureVo = incaeSureService.queryById(sguid);
            map.put("incaeSureVo", incaeSureVo);
        }catch(Exception e){
            log.error("查询对内建安确权记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }
}
